//
//  PictureScrollViewCell.m
//  GameBoss
//
//  Created by dllo on 15/4/22.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "PictureScrollViewCell.h"
#import "UIImageView+WebCache.h"

#import "DataViewController.h"

#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height

@interface PictureScrollViewCell()

@property(nonatomic, assign)BOOL isRefresh;

@end

@implementation PictureScrollViewCell

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.myArr = [NSMutableArray array];
        self.pictureArr = [NSMutableArray array];
        
    // 初始化 最外层scrollview
        self.myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 375 * Screen.width / 375, 200 * Screen.height / 667)];
        self.myScrollView.backgroundColor = [UIColor blackColor];
        [self.contentView addSubview:self.myScrollView];
        [_myScrollView release];
        self.isRefresh = NO;
        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 滚动
    self.myScrollView.contentSize = CGSizeMake(WIDTH * 7, HEIGHT);
    

    
    for (int i = 0;  i < self.pictureArr.count; i++) {
//        UIImage *image = self.pictureArr[i];
        FocusModel *model = [[FocusModel alloc] init];
        model = self.pictureArr[i];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        
        [imageView sd_setImageWithURL:[NSURL URLWithString:model.image]];
        
        
        
//        imageView.image = image;
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT)];
        [scrollView addSubview:imageView];
        [self.myArr addObject:scrollView];
        [self.myScrollView addSubview:scrollView];
        
        // 通过手势来获取当前model,实现跳转
        imageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        [imageView addGestureRecognizer:tapGesture];
        
        [tapGesture release];
        
//        [model release];
        [imageView release];
        [scrollView release];
    }
    
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(250 * Screen.width / 375, 130 * Screen.height / 667, 100 * Screen.width / 375, 40 * Screen.height / 667)];
    self.pageControl.numberOfPages = 5;
    self.pageControl.pageIndicatorTintColor = [UIColor yellowColor];
    self.pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
    [self.contentView addSubview:self.pageControl];
    [_pageControl release];
    
    [self.pageControl addTarget:self action:@selector(click) forControlEvents:UIControlEventValueChanged];
    
    
    self.myScrollView.delegate = self;
    
    self.myScrollView.contentOffset = CGPointMake(WIDTH, 0);
    self.myScrollView.pagingEnabled = YES;
    self.myScrollView.bounces = NO;
    
    
    
    if (!self.isRefresh) {
        // 定时器
        [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(turnChange) userInfo:nil repeats:YES];
        self.isRefresh = YES;
    }
    
}

- (void)tapAction
{
    NSInteger dx = self.myScrollView.contentOffset.x;
    NSInteger num = dx / 375;
    
    FocusModel *model = self.pictureArr[num + 1];
//    NSLog(@"%@", model.url);
    
// cell中的model传值, 第二步
    if ([self.target respondsToSelector:@selector(didTapScrollViewWith:)]) {
        [self.target didTapScrollViewWith:model];
    }
// cell中的model传值, 第二步(评测)
    if ([self.targetEvaluate respondsToSelector:@selector(didTapScrollViewWithEvlu:)]) {
        [self.targetEvaluate didTapScrollViewWithEvlu:model];
    }
//    DataViewController *dataView = [[DataViewController alloc] init];
//    [dataView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
//    [self presentViewController:dataView animated:YES completion:^{
//        
//        
//    }];
    
    
//    [self.navigationController pushViewController:dataView animated:YES];
    //    self.blockNum(model);
    
//    self.myModel = model;
    
}

-(void)turnChange
{
    NSInteger dx = self.myScrollView.contentOffset.x;
    NSInteger num = dx / self.contentView.frame.size.width;
    self.myScrollView.contentOffset = CGPointMake(dx + WIDTH, 0);
//    NSLog(@"%.2f", self.myScrollView.contentOffset.x);
    if (num != 6) {
        
        self.myScrollView.contentOffset = CGPointMake(dx + WIDTH, 0);
    }
    else
    {
        self.myScrollView.contentOffset = CGPointMake(0, 0);
    }
//    NSLog(@"%ld", num);
//    if (num == 0) {
//        self.myScrollView.contentOffset = CGPointMake(WIDTH, 0);
//    }
//    if (num == 5) {
//        self.myScrollView.contentOffset = CGPointMake(WIDTH, 0);
//        num = 0;
//    }
    if (num == 5) {
        self.myScrollView.contentOffset = CGPointMake(WIDTH * 1, 0);
    }
    self.pageControl.currentPage = num;
    if (num == 5) {
        self.pageControl.currentPage = 0;
        return;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger dx = self.myScrollView.contentOffset.x;
    NSInteger num = dx / self.contentView.frame.size.width;
    
//    self.blockNum(num);
    
    // 判断 是否 是两头图片 然后跳转
    if(num == 0)
    {
        self.myScrollView.contentOffset = CGPointMake(5 * WIDTH, 0);
    }
    else if (num == 6)
    {
        self.myScrollView.contentOffset = CGPointMake(WIDTH, 0);
    }
    // pageControl
    if (num == 0) {
        self.pageControl.currentPage = 5;
        return;
    }
    if (num == 6) {
        self.pageControl.currentPage = 0;
    }
    else
    {
        self.pageControl.currentPage = num - 1;
    }


}
// page方法  与图片关联
- (void)click
{
    NSInteger num = self.pageControl.currentPage;
    self.myScrollView.contentOffset = CGPointMake(WIDTH * (num + 1), 0);
}


@end
